Ext.define('PJ.view.celaquestionnairemanage.SetQuestionsView', {
    extend: 'AM.default.DefaultWindowForm',
    requires: ['AM.default.jsonp.DefaultLoadJsonp'],
    
    layout: "fit",
    width: 500,
    atuoHeight: true,
    notEdit: true,
    selectData: null,
    fromPaper: false,
    
    initComponent: function(){
        var bt = "";
        var me = this;
        var tc = "options";
        var fz = "fzOptions"
        this.items = [{
            xtype: "container",
            anchor: "90%",
            margin: "15 0 10 0",
            layout: "column",
            defaults: {
                labelWidth: 80,
                labelAlign: "right",
                columnWidth: 1,
                padding: 5,
                allowBlank: true,
                readOnly:true
            },
            items: [{
                fieldLabel: bt + "试题题型",
                name: "questionType",
                xtype: "combo",
                columnWidth: .5,
                valueField: "value",
                displayField: "text",
                editable: false,
                queryMode: 'local', //'remote',
                value: "danxt",
                store: Ext.create("Ext.data.ArrayStore", {
                    singleton: true,
                    fields: [{
                        name: 'value',
                        mapping: "dataCode",
                        type: 'string'
                    }, {
                        name: 'text',
                        mapping: "dataName",
                        type: 'string'
                    }],
                    proxy: {
                        type: gj_onlineLearning_constant.loadType == "jsonp" ? gj_onlineLearning_constant.loadType:'ajax',
                        url : me.grid.getStore().typeUrl,
                        reader: Ext.create('Ext.data.reader.Json', {
                            type: 'json',
                            root: 'data',
                            successProperty: 'success'
                        })
                    },
                    autoLoad: true
                }),
                listeners: {
                    change: function(f, n, o){
                        me.itemTypeChange(n);
                    }
                }
            }, {
                fieldLabel: bt + "试题题干",
                name: 'questionName',
                xtype: 'textareafield',
                rows: 6,
                maxLength: 120,
                maxLengthText: "题干最多可以输入120个中文字符或240个英文字符。",
                listeners: {
                    blur: function(){
                        this.setValue(this.getValue().trim());
                    }
                }
            }, {
                fieldLabel: bt + "分值",
                xtype: "fieldcontainer",
                itemId: "fzID",
                id: "fzID",
                hidden: false,
                readOnly: true,
                layout: "column",
                margin: "5 5 0 5",
                defaults: {
                    xtype: "combo",
                    store: Ext.create("Ext.data.ArrayStore", {
                        fields: ['value', 'text'],
                        data: [['1', '1分'], ['2', '2分'], ['3', '3分'], ['4', '4分'], ['5', '5分']]
                    }),
                    valueField: 'value',
                    displayField: 'text',
                    editable: false,
                    readOnly:true,
                    emptyText: "请选择",
                    labelWidth: 15,
                    margin: "5 0",
                    columnWidth: 1,
                    listeners: {
                        blur: function(item){
                            if (!Ext.isEmpty(item.getValue()) && item.tid) {
                                if (item.tid < 5) {
                                    var nxt = this.up("fieldcontainer").down("#" + fz + (Number(item.tid) + 1)).enable();
                                }
                            }
                        }
                    }
                },
                items: [{
                    name: tc,
                    fieldLabel: "A",
                    margin: "0 0 5 0",
                    itemId: fz + "0",
                    id: fz + "0",
                    tid: "0",
                    maxLength: 500,
                    maxLengthText: "该选项最多可以输入250个中文字符或500个英文字符。",
                    allowBlank: true
                }, {
                    name: tc,
                    fieldLabel: "B",
                    itemId: fz + "1",
                    tid: "1",
                    maxLength: 500,
                    maxLengthText: "该选项最多可以输入250个中文字符或500个英文字符。",
                    disabled: true
                }, {
                    name: tc,
                    fieldLabel: "C",
                    itemId: fz + "2",
                    tid: "2",
                    maxLength: 500,
                    maxLengthText: "该选项最多可以输入250个中文字符或500个英文字符。",
                    disabled: true
                }, {
                    name: tc,
                    fieldLabel: "D",
                    itemId: fz + "3",
                    tid: "3",
                    maxLength: 500,
                    maxLengthText: "该选项最多可以输入250个中文字符或500个英文字符。",
                    disabled: true
                }, {
                    name: tc,
                    fieldLabel: "E",
                    itemId: fz + "4",
                    tid: "4",
                    maxLength: 500,
                    maxLengthText: "该选项最多可以输入250个中文字符或500个英文字符。",
                    disabled: true
                }]
            }, {
                fieldLabel: bt + "选项",
                xtype: "fieldcontainer",
                itemId: "tcID",
                id: "tcID",
                layout: "column",
                margin: "5 5 0 5",
                defaults: {
                    xtype: "textfield",
                    labelWidth: 15,
                    margin: "5 0",
                    columnWidth: 1,
                    readOnly:true,
                    listeners: {
                        blur: function(item){
                            var option = this.up("fieldcontainer").down("#" + tc + (Number(item.tid)));
                            option.setValue(option.getValue().trim());
                            if (!Ext.isEmpty(item.getValue()) && item.tid) {
                                if (item.tid < 9) {
                                    var nxt = this.up("fieldcontainer").down("#" + tc + (Number(item.tid) + 1)).enable();
                                }
                                if (item.tid < 6) {
                                    //this.up("form").down("#ans" + item.tid).enable();
                                }
                            }
                        }
                    }
                },
                items: [{
                    name: tc,
                    fieldLabel: "A",
                    margin: "0 0 5 0",
                    itemId: tc + "0",
                    id: tc + "0",
                    tid: "0",
                    maxLength: 500,
                    maxLengthText: "该选项最多可以输入250个中文字符或500个英文字符。"
                }, {
                    name: tc,
                    fieldLabel: "B",
                    itemId: tc + "1",
                    tid: "1",
                    maxLength: 500,
                    maxLengthText: "该选项最多可以输入250个中文字符或500个英文字符。",
                    disabled: true
                }, {
                    name: tc,
                    fieldLabel: "C",
                    itemId: tc + "2",
                    tid: "2",
                    maxLength: 500,
                    maxLengthText: "该选项最多可以输入250个中文字符或500个英文字符。",
                    disabled: true
                }, {
                    name: tc,
                    fieldLabel: "D",
                    itemId: tc + "3",
                    tid: "3",
                    maxLength: 500,
                    maxLengthText: "该选项最多可以输入250个中文字符或500个英文字符。",
                    disabled: true
                }, {
                    name: tc,
                    fieldLabel: "E",
                    itemId: tc + "4",
                    tid: "4",
                    maxLength: 500,
                    maxLengthText: "该选项最多可以输入250个中文字符或500个英文字符。",
                    disabled: true
                }, {
                    name: tc,
                    fieldLabel: "F",
                    itemId: tc + "5",
                    tid: "5",
                    maxLength: 500,
                    maxLengthText: "该选项最多可以输入250个中文字符或500个英文字符。",
                    disabled: true
                }, {
                    name: tc,
                    fieldLabel: "G",
                    itemId: tc + "6",
                    tid: "6",
                    maxLength: 500,
                    maxLengthText: "该选项最多可以输入250个中文字符或500个英文字符。",
                    disabled: true
                }, {
                    name: tc,
                    fieldLabel: "H",
                    itemId: tc + "7",
                    tid: "7",
                    maxLength: 500,
                    maxLengthText: "该选项最多可以输入250个中文字符或500个英文字符。",
                    disabled: true
                }, {
                    name: tc,
                    fieldLabel: "I",
                    itemId: tc + "8",
                    tid: "8",
                    maxLength: 500,
                    maxLengthText: "该选项最多可以输入250个中文字符或500个英文字符。",
                    disabled: true
                }, {
                    name: tc,
                    fieldLabel: "J",
                    itemId: tc + "9",
                    tid: "9",
                    maxLength: 500,
                    maxLengthText: "该选项最多可以输入250个中文字符或500个英文字符。",
                    disabled: true
                }]
            }, {
                fieldLabel: "是否限制答案字数",
                labelWidth: 125,
                xtype: "radiogroup",
                itemId: "isLimitNum",
                blankText: "参考答案不允许为空！",
                margin: "5 5 0 5",
                isLimitModel: false,
                items: [{
                    name: "isLimitNum",
                    boxLabel: "是",
                    inputValue: "1",
                    readOnly:true,
                    itemId: "isLimitNum0"
                }, {
                    name: "isLimitNum",
                    boxLabel: "否",
                    inputValue: "2",
                    readOnly:true,
                    itemId: "isLimitNum1",
                    checked: true
                }],
                listeners: {
                    change: function(f, n, o){
                        
                        var form = this.up("form");
                            var wordNum = Ext.getCmp("wordNum1");
                            var wordNum2 = Ext.getCmp("wordNum2");
                            if (n.isLimitNum == 1) {
                            	wordNum.setFieldLabel('<span style="color:red;">*</span>字数限制：字数上限为');
                            	wordNum.setDisabled(false);
                            	wordNum2.setDisabled(false);
                            }
                            else 
                                if (n.isLimitNum == 2) {
                                	wordNum.setFieldLabel('字数限制：字数上限为');
                                	wordNum.setDisabled(true);
                                	wordNum2.setDisabled(true);
                                }
                    },
                    afterrender: function(){
                        this.hide();
                    }
                }
            }, {
				xtype: "numberfield",
				fieldLabel: '字数限制：字数上限为',
				labelSeparator: "",
				name: "wordNum",
				itemId: "wordNum1",
				id: "wordNum1",
				allowDecimals: false,
				hideTrigger: true,
                blankText: "字数限制不允许为空！",
                labelWidth: 135,
                columnWidth: .5,
                margin: "5 5 0 5",
                listeners: {
                    afterrender: function(){
                    	this.setDisabled(true);
                        this.hide();
                    }
                }
			}, {
				xtype: "label",
				text: "个汉字",
				itemId: "wordNum2",
				id: "wordNum2",
				columnWidth: .2,
				margin: "5 0 0 5",
                listeners: {
                    afterrender: function(){
                    	this.setDisabled(true);
                        this.hide();
                    }
                }
			}, {
                xtype: "hidden",
                name: "questionId"
            }]
        }];
        
        this.buttons = [{
            text: '关闭',
            handler: function(button, event){
                var win = button.up('window');
                win.close();
            }
        }];
        
        this.callParent(arguments);
    },
    
    listeners: {
        render: function(){
            this.show();
        },
        beforerender: function(){
            var me = this;
            if (!me.notEdit) {
            	var actionName = "load";//默认使用非JSONP的加载方法名“load”
                if(gj_onlineLearning_constant.loadType=="jsonp") {//根据模块的全局变量设置是否使用JSONP的方式请求数据
                	actionName= "loadJsonp";
                }
                me.down('form').getForm().doAction(actionName,{//使用doAction(action,{options})；方式加载数据
                    url: me.grid.getStore().perUpdateUrl,
                    params: {
                    	questionId:me.questionId
                    },
                    success:function(form,action){
                    	var selectData = action.result.data;//Ext.decode(action.response.responseText).data;
                        me.pushData(selectData);
                    }
                });
            };
                    },
        afterrender: function(){
            this.itemTypeChange(this.resourceItemType);
        }
    },
    
    /**
     * 根据试题类型显示相应控件
     * @param {string} code 控件类型编码
     */
    itemTypeChange: function(code){
		var win = this;
        if (!code) {
            code = "danxt";
        }
        var form = this.down("form");
        var _c = form.down("#tcID");
        var _f = form.down("#fzID");
        var _m = form.down("checkboxgroup");
        //var multiAnswer = form.down("#multiAnswer");
        var wordNum = form.down("#wordNum1");
        var isLimitNum = form.down("#isLimitNum");
        var wordNum2 = form.down("#wordNum2");
        if (code == "danxt") {//单选
        	if(wordNum){
        		wordNum.hide();
        	}
        	if(isLimitNum){
        		isLimitNum.hide();
        	}
        	if(wordNum2){
        		wordNum2.hide();
        	}
            _c.hide();
            _f.show();
            //multiAnswer.show();
            _m.reset();
            _m.singleModel = false;
            //this.selectModel();
            this.clearTemContent();
            Ext.each(_c.items.items, function(item){
                item.reset();
            });
			win.setHeight(375);
			//Ext.getCmp("multiAnswer").allowBlank = false;
			//选项A
    		Ext.getCmp("options0").allowBlank = true;
    		Ext.getCmp("options0").setDisabled(true);
    		Ext.getCmp("fzOptions0").allowBlank = true;//分值A
        }
        else 
            if (code == "duoxt") {//多选
            	if(wordNum){
            		wordNum.hide();
            	}
            	if(isLimitNum){
            		isLimitNum.hide();
            	}
            	if(wordNum2){
            		wordNum2.hide();
            	}
                _c.show();
            	_f.hide();
                //multiAnswer.show();
                _m.reset();
                _m.singleModel = true;
                //this.selectModel();
                this.clearTemContent();
                Ext.each(_c.items.items, function(item){
                    item.reset();
                });
				win.setHeight(550);
				//Ext.getCmp("multiAnswer").allowBlank = false;
				//选项A
        		Ext.getCmp("options0").allowBlank = true;
    			Ext.getCmp("options0").setDisabled(false);
    			Ext.getCmp("fzOptions0").allowBlank = true;//分值A
            }
            else 
                /*if (code == "QETT02") {//判断
                	if(wordNum){
                		wordNum.allowBlank = true;
                		wordNum.hide();
                	}
                	if(isLimitNum){
                		isLimitNum.hide();
                	}
                	if(wordNum2){
                		wordNum2.hide();
                	}
//					_c.setVisible(false);
//                    _m.reset();
                    _m.singleModel = false;
                    this.judegModel();
//                    Ext.each(_c.items.items, function(item){
//                        item.reset();
//                    });
                    _c.hide();
                    //multiAnswer.show();
					win.setHeight(250);
					//Ext.getCmp("multiAnswer").allowBlank = false;
            		Ext.getCmp("itemContent0").allowBlank = false;
//					_c.setVisible(false);
                }else*/ if(code == "wendt"){
            		wordNum.show();
            		isLimitNum.show();
            		wordNum2.show();
            		//Ext.getCmp("multiAnswer").allowBlank = true;
            		//选项A
	        		Ext.getCmp("options0").allowBlank = true;
	        		Ext.getCmp("options0").setDisabled(true);
	        		Ext.getCmp("fzOptions0").allowBlank = true;//分值A
	        		_m.singleModel = false;
                	//multiAnswer.hide();
                	_c.hide();
	                _f.hide();
                	win.setHeight(300);
                }
    },
    
    /**
     * 初始话预修改试题数据
     * @param {json} data
     */
    pushData: function(data){
        var optionList = data.optionList;
        for (var i = 0, lan = optionList.length; i < lan; i++) {
            //var item = this.down("#ans" + i).enable();
            var tmct = this.down("#fzID").down("#fzOptions" + i);
            tmct.enable();
            tmct.setValue(optionList[i].optionName);
            tmct.allowBlank = false;
            tmct.focus();
        }
        
    },
    
    /**
     * 试题显示参考答案模式：判断题模式
     */
    /*
    judegModel: function(){
        this.down("#tcID").down("#itemContent0").allowBlank = true;
        var g = this.down("checkboxgroup");
        for (var i = 0; i < 6; i++) {
            var item = g.down("#ans" + i);
            //if (i > 1) {
		    item.hide();
            //}
            //else 
                if (i == 0) {
                    //item.boxLabelEl.setHTML("对");
                    item.enable();
					item.show();
                }
                else 
                    if (i == 1) {
                        //item.boxLabelEl.setHTML("错");
                        item.enable();
						item.show();
                    }
        }
    },*/
    
    /**
     *  试题显示参考答案模式：选择题模式
     */
    /*
    selectModel: function(){
        this.down("#tcID").down("#itemContent0").allowBlank = false;
        var g = this.down("checkboxgroup");
        for (var i = 0, x = 1; i < 6; i++, x++) {
            var item = g.down("#ans" + i);
            if (i > 1) {
                item.show().disable();
            }
            else 
                if (i == 0) {
                    item.boxLabelEl.setHTML("A");
                }
                else 
                    if (i == 1) {
                        item.boxLabelEl.setHTML("B");
                        item.disable();
                    }
        };
            },*/
    
    /**
     * 清空试题选项内容
     */
    clearTemContent: function(){
        for (var i = 1; i < 10; i++) {
            var tmct = this.down("#tcID").down("#options" + i);
            tmct.disable();
        }
    }
});
